//把二叉搜索树转换为累加树
class Solution {
public:
    //
    int sum=0;
    TreeNode* convertBST(TreeNode* root) 
    {
        if(root==nullptr) return nullptr;
        dfs(root);
        return root;
    }
    void dfs(TreeNode* root)
    {
        if(root==nullptr) return;
        dfs(root->right);
        sum+=root->val;
        root->val=sum;
        dfs(root->left);
    }
};

//平衡二叉树
class Solution {
public:
    bool isBalanced(TreeNode* root) 
    {
        if(root==nullptr) return true;
        int left=getmaxpath(root->left);
        int right=getmaxpath(root->right);
        return abs(left-right)<2 && isBalanced(root->left) && isBalanced(root->right); 
    }
    int getmaxpath(TreeNode* root)
    {
        if(root==nullptr) return 0;
        int left=getmaxpath(root->left);
        int right=getmaxpath(root->right);
        return 1+max(left,right);
    }
};

//二叉树的所有路径
class Solution {
public:
    vector<string>res;
    vector<int>path;
    vector<string> binaryTreePaths(TreeNode* root) 
    {
        if(!root) return res;
        preordered(root);
        return res;
    }
    void preordered(TreeNode* root)
    {

        path.push_back(root->val);
        if(!root->left && !root->right)
        {
            string sPath;
            for (int i = 0; i < path.size() - 1; i++) 
            {
                sPath += to_string(path[i]);
                sPath += "->";
            }
            sPath += to_string(path[path.size() - 1]);
            res.push_back(sPath);
            return;
        }
        if(root->left)
        {
            preordered(root->left);
            path.pop_back();
        }
        if(root->right)
        {
            preordered(root->right);
            path.pop_back();
        }

    }
};